import CardService from '../service/CardService'
import LogUtil from "../utils/LogUtil";

export const NameSpace = 'CardModel';

function createAction(action, payload) {
    return {type: `${NameSpace}/${action}`, payload};
}

export const Actions = {
    asyncActionGetCardList(params) {
        return createAction('asyncGetCardList', params)
    },
};

function actionLoading(loading) {
    return createAction('loading', loading);
}

function actionResult(stat) {
    return createAction('result', stat);
}

function actionList(list) {
    return createAction('list', list);
}

export default {
    namespace: NameSpace,
    state: {
        loading: false,
        stat: '',
        list: null
    },
    reducers: {
        loading(state, {payload}) {
            return {
                ...state,
                loading: !state.list ? payload : false
            }
        },
        list(state, {payload}) {
            return {
                ...state,
                list: payload
            }
        },
    },

    asyncTasks: {
        asyncGetCardList({dispatch}, {payload}) {
            dispatch(actionLoading(true));
            CardService.netGerCardList(payload)
                .then(function (result) {
                    dispatch(actionLoading(false));
                    // 数据
                    dispatch(actionList(result.info));
                    dispatch(actionResult(1));
                })
                .catch(function (error) {
                    dispatch(actionResult(0));
                    dispatch(actionLoading(false));
                    LogUtil.err(error);
                });
        },
    }
}